// 订单管理页面
const app = getApp();
const request = require('../../../utils/request.js').request;

Page({
  data: {
    loading: true,
    orders: [],
    activeTab: 0,
    tabs: [
      { title: '全部', type: null, status: null },
      { title: '门票订单', type: 1, status: null },
      { title: '酒店订单', type: 2, status: null },
      { title: '待支付', type: null, status: 1 },
      { title: '已完成', type: null, status: 3 }
    ],
    showDetail: false,
    currentOrder: null
  },

  onLoad: function (options) {
    this.loadOrders();
  },

  onPullDownRefresh: function () {
    this.loadOrders();
  },

  // 切换选项卡
  onTabChange: function (e) {
    const index = e.currentTarget.dataset.index;
    this.setData({ activeTab: index });
    this.loadOrders();
  },

  // 加载订单列表
  loadOrders: function () {
    this.setData({ loading: true });
    
    const activeTab = this.data.activeTab;
    if (activeTab === undefined || !this.data.tabs || !this.data.tabs[activeTab]) {
      this.setData({ 
        loading: false,
        activeTab: 0
      });
      return;
    }
    
    const { type, status } = this.data.tabs[activeTab];
    let url = '/api/order/all';
    let params = {};
    
    if (type !== null) {
      params.type = type;
    }
    
    if (status !== null) {
      params.status = status;
    }
    
    request({
      url: url,
      method: 'GET',
      data: params,
      success: (res) => {
        if (res.code === 0) {
          this.setData({
            orders: res.data,
            loading: false
          });
        } else {
          wx.showToast({
            title: res.msg || '获取订单列表失败',
            icon: 'none'
          });
          this.setData({ loading: false });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
        this.setData({ loading: false });
      },
      complete: () => {
        wx.stopPullDownRefresh();
      }
    });
  },

  // 显示订单详情
  showOrderDetail: function (e) {
    const index = e.currentTarget.dataset.index;
    const order = this.data.orders[index];
    
    this.setData({
      showDetail: true,
      currentOrder: order
    });
  },

  // 关闭详情
  closeDetail: function () {
    this.setData({
      showDetail: false
    });
  },

  // 更新订单状态
  updateOrderStatus: function (e) {
    const { id, status } = e.currentTarget.dataset;
    
    wx.showModal({
      title: '确认操作',
      content: '确定要更新订单状态吗？',
      success: (res) => {
        if (res.confirm) {
          request({
            url: `/api/order/${id}/status`,
            method: 'PUT',
            data: { status },
            success: (res) => {
              if (res.code === 0) {
                wx.showToast({
                  title: '更新成功',
                  icon: 'success'
                });
                this.setData({ showDetail: false });
                this.loadOrders();
              } else {
                wx.showToast({
                  title: res.msg || '更新失败',
                  icon: 'none'
                });
              }
            },
            fail: () => {
              wx.showToast({
                title: '网络错误',
                icon: 'none'
              });
            }
          });
        }
      }
    });
  },

  // 取消订单
  cancelOrder: function (e) {
    const id = e.currentTarget.dataset.id;
    
    wx.showModal({
      title: '确认取消',
      content: '确定要取消此订单吗？',
      success: (res) => {
        if (res.confirm) {
          request({
            url: `/api/order/${id}/cancel`,
            method: 'PUT',
            success: (res) => {
              if (res.code === 0) {
                wx.showToast({
                  title: '取消成功',
                  icon: 'success'
                });
                this.setData({ showDetail: false });
                this.loadOrders();
              } else {
                wx.showToast({
                  title: res.msg || '取消失败',
                  icon: 'none'
                });
              }
            },
            fail: () => {
              wx.showToast({
                title: '网络错误',
                icon: 'none'
              });
            }
          });
        }
      }
    });
  },

  // 删除订单
  deleteOrder: function (e) {
    const id = e.currentTarget.dataset.id;
    
    wx.showModal({
      title: '确认删除',
      content: '确定要删除此订单吗？该操作不可恢复！',
      success: (res) => {
        if (res.confirm) {
          request({
            url: `/api/order/${id}`,
            method: 'DELETE',
            success: (res) => {
              if (res.code === 0) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'success'
                });
                this.setData({ showDetail: false });
                this.loadOrders();
              } else {
                wx.showToast({
                  title: res.msg || '删除失败',
                  icon: 'none'
                });
              }
            },
            fail: () => {
              wx.showToast({
                title: '网络错误',
                icon: 'none'
              });
            }
          });
        }
      }
    });
  }
}) 